using System;
using System.Data;
using System.Xml;
using System.Runtime.Serialization;
using System.Collections;
using Widgetsphere.Core.Exceptions;
using Widgetsphere.Core.DataAccess;
using ACME.Northwind.Business;
using ACME.Northwind.Business.Objects;
using ACME.Northwind.Business.Rules;
using ACME.Northwind.Business.SelectCommands;
using System.Collections.Generic;

namespace ACME.Northwind.Domain.Objects
{
	/// <summary>
	/// This is an customizable extender for the domain class associated with the 'Employee' object
	/// </summary>
	[Serializable()]
	partial class DomainEmployee : PersistableDomainObjectBase
	{
		#region Member Variables

		private const string TABLE_NAME = "EmployeeCollection";
		internal DomainEmployeeCollection ParentCol;

		#endregion

		#region Constructor

		internal DomainEmployee(DataRowBuilder rb) : base(rb) 
		{
			this.ParentCol = ((DomainEmployeeCollection)(base.Table));
		}

		#endregion

		#region Properties

		internal EmployeePrimaryKey PrimaryKey
		{
			get { return new EmployeePrimaryKey(this.EmployeeId); }
		}

		internal string Address
		{
			get
			{
				try
				{
					if (base[this.ParentCol.AddressColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.AddressColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.AddressColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.AddressColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.Address = value;
				}
			}
		}

		internal DateTime? BirthDate
		{
			get
			{
				try
				{
					if (base[this.ParentCol.BirthDateColumn] == System.DBNull.Value) return null;
					else return ((DateTime?)(base[this.ParentCol.BirthDateColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.BirthDateColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.BirthDateColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.BirthDate = value;
				}
			}
		}

		internal string City
		{
			get
			{
				try
				{
					if (base[this.ParentCol.CityColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.CityColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.CityColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.CityColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.City = value;
				}
			}
		}

		internal string Country
		{
			get
			{
				try
				{
					if (base[this.ParentCol.CountryColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.CountryColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.CountryColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.CountryColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.Country = value;
				}
			}
		}

		internal int EmployeeId
		{
			get
			{
				try
				{
					return ((int)(base[this.ParentCol.EmployeeIdColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
		}

		internal string Extension
		{
			get
			{
				try
				{
					if (base[this.ParentCol.ExtensionColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.ExtensionColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.ExtensionColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.ExtensionColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.Extension = value;
				}
			}
		}

		internal string FirstName
		{
			get
			{
				try
				{
					if (base[this.ParentCol.FirstNameColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.FirstNameColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  if (value == null) throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_SETNULL);

			  if (!base[this.ParentCol.FirstNameColumn].Equals(value))
				{
					base[this.ParentCol.FirstNameColumn] = value;
			  	wasSet = true;
				}

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.FirstName = value;
				}
			}
		}

		internal DateTime? HireDate
		{
			get
			{
				try
				{
					if (base[this.ParentCol.HireDateColumn] == System.DBNull.Value) return null;
					else return ((DateTime?)(base[this.ParentCol.HireDateColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.HireDateColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.HireDateColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.HireDate = value;
				}
			}
		}

		internal string HomePhone
		{
			get
			{
				try
				{
					if (base[this.ParentCol.HomePhoneColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.HomePhoneColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.HomePhoneColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.HomePhoneColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.HomePhone = value;
				}
			}
		}

		internal string LastName
		{
			get
			{
				try
				{
					if (base[this.ParentCol.LastNameColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.LastNameColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  if (value == null) throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_SETNULL);

			  if (!base[this.ParentCol.LastNameColumn].Equals(value))
				{
					base[this.ParentCol.LastNameColumn] = value;
			  	wasSet = true;
				}

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.LastName = value;
				}
			}
		}

		internal string Notes
		{
			get
			{
				try
				{
					if (base[this.ParentCol.NotesColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.NotesColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.NotesColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.NotesColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.Notes = value;
				}
			}
		}

		internal System.Byte[] Photo
		{
			get
			{
				try
				{
					if (base[this.ParentCol.PhotoColumn] == System.DBNull.Value) return null;
					else return ((System.Byte[])(base[this.ParentCol.PhotoColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.PhotoColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.PhotoColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.Photo = value;
				}
			}
		}

		internal string PhotoPath
		{
			get
			{
				try
				{
					if (base[this.ParentCol.PhotoPathColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.PhotoPathColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.PhotoPathColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.PhotoPathColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.PhotoPath = value;
				}
			}
		}

		internal string PostalCode
		{
			get
			{
				try
				{
					if (base[this.ParentCol.PostalCodeColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.PostalCodeColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.PostalCodeColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.PostalCodeColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.PostalCode = value;
				}
			}
		}

		internal string Region
		{
			get
			{
				try
				{
					if (base[this.ParentCol.RegionColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.RegionColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.RegionColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.RegionColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.Region = value;
				}
			}
		}

		internal int? ReportsTo
		{
			get
			{
				try
				{
					if (base[this.ParentCol.ReportsToColumn] == System.DBNull.Value) return null;
					else return ((int?)(base[this.ParentCol.ReportsToColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.ReportsToColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.ReportsToColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.ReportsTo = value;
				}
			}
		}

		internal string Title
		{
			get
			{
				try
				{
					if (base[this.ParentCol.TitleColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.TitleColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.TitleColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.TitleColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.Title = value;
				}
			}
		}

		internal string TitleOfCourtesy
		{
			get
			{
				try
				{
					if (base[this.ParentCol.TitleOfCourtesyColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.TitleOfCourtesyColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.TitleOfCourtesyColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.TitleOfCourtesyColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					EmployeeCollection list = (EmployeeCollection)this.ParentCol.SubDomain[Collections.EmployeeCollection];
					Employee item = list.GetItemByPK(this.EmployeeId);
					if (item != null) item.TitleOfCourtesy = value;
				}
			}
		}

		internal virtual BusinessObjectList<EmployeeTerritory> EmployeeTerritoryList
		{
			get
			{
				BusinessObjectList<EmployeeTerritory> associatedEmployeeTerritoryList = null;
				//if (associatedEmployeeTerritoryList == null)
				//{
					associatedEmployeeTerritoryList = new BusinessObjectList<EmployeeTerritory>();
					foreach (DomainEmployeeTerritory EmployeeTerritory in base.GetChildRows(ParentCol.EmployeeEmployeeTerritoryRelation))
					{
						if (EmployeeTerritory.RowState != DataRowState.Deleted)
							associatedEmployeeTerritoryList.Add(new EmployeeTerritory(EmployeeTerritory));
					}
					associatedEmployeeTerritoryList.ObjectAdded += new BusinessObjectEventHandler(AssociatedEmployeeTerritoryList_ObjectAdded);
					associatedEmployeeTerritoryList.ObjectRemoved += new BusinessObjectEventHandler(AssociatedEmployeeTerritoryList_ObjectRemoved);
				//}
				return associatedEmployeeTerritoryList;
			}
		}

		internal virtual BusinessObjectList<Order> OrderList
		{
			get
			{
				BusinessObjectList<Order> associatedOrderList = null;
				//if (associatedOrderList == null)
				//{
					associatedOrderList = new BusinessObjectList<Order>();
					foreach (DomainOrder Order in base.GetChildRows(ParentCol.EmployeeOrderRelation))
					{
						if (Order.RowState != DataRowState.Deleted)
							associatedOrderList.Add(new Order(Order));
					}
					associatedOrderList.ObjectAdded += new BusinessObjectEventHandler(AssociatedOrderList_ObjectAdded);
					associatedOrderList.ObjectRemoved += new BusinessObjectEventHandler(AssociatedOrderList_ObjectRemoved);
				//}
				return associatedOrderList;
			}
		}

		#endregion

		#region Collection Operation Methods

		/// <summary>
		/// Serves as a hash function for this particular type.
		/// </summary>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj == null) return false;
			if (obj.GetType() != this.GetType()) return false;

			if ((this.RowState == DataRowState.Deleted) ||
				(((DataRow)obj).RowState == DataRowState.Deleted) ||
				(this.RowState == DataRowState.Detached) ||
				(((DataRow)obj).RowState == DataRowState.Detached))
				return false;

			return ((DomainEmployee)obj).PrimaryKey.Equals(this.PrimaryKey);
		}

		public override void Persist()
		{
			DomainHelper.PersistDomainObject(this, this.ParentCol.SubDomain, Collections.EmployeeCollection, this.ParentCol.Modifier);
		}

		public override void Remove()
		{
			this.ParentCol.RemoveEmployee(this);
		}

		#endregion

		#region Relationship Methods
		private void AssociatedEmployeeTerritoryList_ObjectAdded(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainEmployeeTerritory addedItem = (DomainEmployeeTerritory)e.BusinessObject.WrappedClass;
			DomainEmployeeTerritoryCollection existingEmployeeTerritoryCollection = (DomainEmployeeTerritoryCollection)this.ParentCol.SubDomain.GetDomainCollection(Collections.EmployeeTerritoryCollection);
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			if(addedItem.ParentCol != existingEmployeeTerritoryCollection)
			{
				this.ParentCol.SubDomain.Merge(new DataRow[] { addedItem } , false, MissingSchemaAction.Error);
				addedItem = existingEmployeeTerritoryCollection.GetEmployeeTerritory(addedItem.EmployeeId, addedItem.TerritoryId);
				e.BusinessObject.WrappedClass = addedItem;
			}
			addedItem.SetParentRow(this,this.ParentCol.EmployeeEmployeeTerritoryRelation);
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		private void AssociatedEmployeeTerritoryList_ObjectRemoved(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainEmployeeTerritory removedItem = (DomainEmployeeTerritory)e.BusinessObject.WrappedClass;
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			removedItem.Delete();
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		private void AssociatedOrderList_ObjectAdded(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainOrder addedItem = (DomainOrder)e.BusinessObject.WrappedClass;
			DomainOrderCollection existingOrderCollection = (DomainOrderCollection)this.ParentCol.SubDomain.GetDomainCollection(Collections.OrderCollection);
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			if(addedItem.ParentCol != existingOrderCollection)
			{
				this.ParentCol.SubDomain.Merge(new DataRow[] { addedItem } , false, MissingSchemaAction.Error);
				addedItem = existingOrderCollection.GetOrder(addedItem.OrderId);
				e.BusinessObject.WrappedClass = addedItem;
			}
			addedItem.SetParentRow(this,this.ParentCol.EmployeeOrderRelation);
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		private void AssociatedOrderList_ObjectRemoved(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainOrder removedItem = (DomainOrder)e.BusinessObject.WrappedClass;
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			removedItem.EmployeeId = null;
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}


		private bool retrievedEmployeeChildren = false;
		public BusinessObjectList<Employee> ChildEmployeeList
		{
			get
			{
				if(!retrievedEmployeeChildren)
				{
					List<EmployeePrimaryKey> list = new List<EmployeePrimaryKey>();
					list.Add(this.PrimaryKey);
					this.ParentCol.SubDomain.AddSelectCommand(new EmployeeSelectByPks(list, RecurseDirection.DOWN, 1));
					this.ParentCol.SubDomain.RunSelectCommands();
					retrievedEmployeeChildren = true;
				}
				BusinessObjectList<Employee> associatedChildList = new BusinessObjectList<Employee>();
				DataRow[] childRecords = this.ParentCol.Select("ReportsTo = " + this.EmployeeId);
				foreach(DomainEmployee employee in childRecords)
				{
					if (employee.RowState != DataRowState.Deleted)
						associatedChildList.Add(new Employee(employee));
				}

				associatedChildList.ObjectAdded += new BusinessObjectEventHandler(AssociatedChildEmployeeList_ObjectAdded);
				associatedChildList.ObjectRemoved += new BusinessObjectEventHandler(AssociatedChildEmployeeList_ObjectRemoved);
				return associatedChildList;
			}
		}

		private void AssociatedChildEmployeeList_ObjectAdded(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainEmployee addedItem = (DomainEmployee)e.BusinessObject.WrappedClass;
			DomainEmployeeCollection existingEmployeeCollection = (DomainEmployeeCollection)this.ParentCol.SubDomain.GetDomainCollection(Collections.EmployeeCollection);
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			if(addedItem.ParentCol != existingEmployeeCollection)
			{
				this.ParentCol.SubDomain.Merge(new DataRow[] { addedItem } , false, MissingSchemaAction.Error);
				addedItem = existingEmployeeCollection.GetEmployee(addedItem.EmployeeId);
				e.BusinessObject.WrappedClass = addedItem;
			}
			addedItem.ReportsTo = this.ReportsTo;
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		private void AssociatedChildEmployeeList_ObjectRemoved(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainEmployee removedItem = (DomainEmployee)e.BusinessObject.WrappedClass;
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			removedItem.ReportsTo = null;
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		public DomainEmployee ParentEmployeeItem
		{
			get
			{
				DomainEmployee returnVal = null;
				if ((this.ReportsTo != null))
				{
					returnVal = this.ParentCol.GetEmployee(this.ReportsTo.Value);
					if(returnVal == null)
					{
						EmployeePrimaryKey primaryKey = new EmployeePrimaryKey(this.ReportsTo.Value);
						List<EmployeePrimaryKey> list = new List<EmployeePrimaryKey>();
						list.Add(primaryKey);
						this.ParentCol.SubDomain.AddSelectCommand(new EmployeeSelectByPks(list));
						this.ParentCol.SubDomain.RunSelectCommands();
						returnVal = this.ParentCol.GetEmployee(this.ReportsTo.Value);
					}
				}
				return returnVal;
			}
			set
			{
				this.ReportsTo = value.ReportsTo;
			}
		}		
		internal virtual void ReleaseNonIdentifyingRelationships()
		{
			this.OrderList.Clear();
		}

		#endregion
	}
}
